package response

import (
	"net/http"

	"github.com/labstack/echo/v4"
)

// Response 统一的API响应结构
type Response struct {
	Code    int         `json:"code"`              // 错误码，如果成功则为0
	Message string      `json:"message,omitempty"` // 用户可读的错误消息
	Detail  interface{} `json:"detail,omitempty"`  // 额外的错误详情
	Data    interface{} `json:"data,omitempty"`
}

// PaginatedData 分页数据结构
type PaginatedData struct {
	Rows       []interface{} `json:"rows"`
	Total      int64         `json:"total"`
	Page       int           `json:"page"`
	Limit      int           `json:"limit"`
	TotalPages int64         `json:"total_pages"`
}

// 成功响应构造函数

// Success 创建成功响应
func Success(c echo.Context, data interface{}) error {
	resp := &Response{
		Data: data,
	}
	return c.JSON(http.StatusOK, resp)
}

// Paginated 创建分页响应
func Paginated(c echo.Context, rows []interface{}, total int64, page, limit int) error {
	totalPages := (total + int64(limit) - 1) / int64(limit)
	if totalPages <= 0 {
		totalPages = 0
	}

	return c.JSON(http.StatusOK, &Response{
		Data: &PaginatedData{
			Rows:       rows,
			Total:      total,
			Page:       page,
			Limit:      limit,
			TotalPages: totalPages,
		},
	})
}

// BadRequest 创建400错误响应
func BadRequest(c echo.Context, message string) error {
	return c.JSON(http.StatusBadRequest, &Response{
		Code:    http.StatusBadRequest,
		Message: message,
	})
}

// Unauthorized 创建401错误响应
func Unauthorized(c echo.Context, message string) error {
	return c.JSON(http.StatusUnauthorized, &Response{
		Code:    http.StatusUnauthorized,
		Message: message,
	})
}

// Forbidden 创建403错误响应
func Forbidden(c echo.Context, message string) error {
	return c.JSON(http.StatusForbidden, &Response{
		Code:    http.StatusForbidden,
		Message: message,
	})
}

// NotFound 创建404错误响应
func NotFound(c echo.Context, message string) error {
	return c.JSON(http.StatusNotFound, &Response{
		Code:    http.StatusNotFound,
		Message: message,
	})
}

// Conflict 创建409错误响应
func Conflict(c echo.Context, message string) error {
	return c.JSON(http.StatusConflict, &Response{
		Code:    http.StatusConflict,
		Message: message,
	})
}

// UnprocessableEntity 创建422错误响应
func UnprocessableEntity(c echo.Context, message string) error {
	return c.JSON(http.StatusUnprocessableEntity, &Response{
		Code:    http.StatusUnprocessableEntity,
		Message: message,
	})
}

// TooManyRequests 创建429错误响应
func TooManyRequests(c echo.Context, message string) error {
	return c.JSON(http.StatusTooManyRequests, &Response{
		Code:    http.StatusTooManyRequests,
		Message: message,
	})
}

// InternalServerError 创建500错误响应
func InternalServerError(c echo.Context, message string) error {
	return c.JSON(http.StatusInternalServerError, &Response{
		Code:    http.StatusInternalServerError,
		Message: message,
	})
}

// MethodNotAllowed 创建405错误响应
func MethodNotAllowed(c echo.Context, message string) error {
	return c.JSON(http.StatusMethodNotAllowed, &Response{
		Code:    http.StatusMethodNotAllowed,
		Message: message,
	})
}

// ServiceUnavailable 创建503错误响应
func ServiceUnavailable(c echo.Context, message string) error {
	return c.JSON(http.StatusServiceUnavailable, &Response{
		Code:    http.StatusServiceUnavailable,
		Message: message,
	})
}

// BadGateway 创建502错误响应
func BadGateway(c echo.Context, message string) error {
	return c.JSON(http.StatusBadGateway, &Response{
		Code:    http.StatusBadGateway,
		Message: message,
	})
}
